package org.bud.rung.netty.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.bud.rung.bit.JobMethod;
import org.bud.rung.bit.JobRequest;
import org.bud.rung.bit.R;
import org.bud.rung.constant.Constant;

import java.lang.reflect.Method;

/**
 * @author shaun
 * @date 2024/8/10
 * @instruction
 */
public class ServerInHandler extends SimpleChannelInboundHandler<JobRequest> {
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, JobRequest jobRequest) throws Exception {
        JobMethod jobMethod = Constant.jobMethodCache.get(jobRequest.getJobName());
        if(jobMethod == null){
            channelHandlerContext.writeAndFlush(R.jobEmpty());
            return;
        }
        channelHandlerContext.writeAndFlush(R.ok(
                (String)jobMethod.getMethod().invoke(jobMethod.getBean()),
                Constant.jobMethodCache.get(jobMethod.getJobName()).getLast().toString()
        ));
    }
}
